<?php
require 'includes/db.php';
if (isset($_POST['submit'])) {

	$name = $_POST['name'];
	$p_image = $_FILES['image']['name'];
	$p_image = date('ymdHis') . '_' . uniqid() . '_' . $p_image;
	$fileInfoArray = pathinfo($p_image);

	$src = $_FILES['image']['tmp_name'];
	$dest = "images/iphone/$p_image";
	$isMoved = @move_uploaded_file($src, $dest);

	$_POST['image'] = $p_image;

	$abc = mysql_query("INSERT INTO iphone(name,image) VALUES ('$name','$p_image')") or die(mysql_error());

	header('location:iphone_development.php');
}

require 'header.php';
?>
<script src="js/jquery-1.8.2.min.js" type="text/javascript"></script>
<script src="js/jquery.validate.js" type="text/javascript"></script>
<script src="js/register.jquery.js" type="text/javascript"></script>
<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <form id="registerId" action="" method="post" enctype="multipart/form-data">
        <table class="table table-striped">
            <tbody>
                 <tr>
                    <td width="20%" align="right">Name :</td>
                    <td width="40%" align="right">
                        <input class="form-control" type="text" name="name" value="" />
                    </td>
                    <td width="40%" align="right"><label for="name" generated="true" class="error"></label></td>
                </tr>
                <tr>
                    <td align="right">Upload Image :</td>
                    <td >
                        <input class="form-control" type="file" name="image" value="" placeholder="My avatar is..." />       </td>
                    <td><label for="image" generated="true" class="error"></label></td>
                </tr>
                <tr>
                    <td align="right">&nbsp;</td>
                    <td >
                        <input class="form-control" type="submit" name="submit" value="Upload Image" />        </td>
                    <td align="right">&nbsp;</td>
                </tr>

            </tbody>

        </table>
    </form>
</div>
<?php
$tableName = 'iphone';

$where = '';
$id = isset($_GET['id']) ? $_GET['id'] : 0;
$total_users = CountAll('iphone');

########### Count total records in users table ##############

########### PAGING START ###########################

$rpp = 4;
// Record Per Page (offset)
$total_pages = ceil($total_users / $rpp);
$currentPage = isset($_GET['page']) ? $_GET['page'] : 1;
$index = $rpp * ($currentPage - 1);
// limit or from where we have to start fetching
?>
<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
     <?php
	if (isset($_GET['mode'])) {
		$query = mysql_query("select image from iphone where id='$id'");
		$image_name = mysql_fetch_array($query);
		$deleteFile = $image_name['image'];
		@unlink('images/iphone/' . $deleteFile);
		$numRows = deleteby($tableName, $id);
		if ($numRows == 1) {
			echo success("Succefully Deleted");
		}
	}
	if (isset($_GET['status'])) {
		$newStatus = $_GET['status'] == 0 ? 1 : 0;
		$data = array('status' => $newStatus);
		$flag = updateById($tableName, $data, $id);
		echo success("Successfully Upadated #id $id");
	}
    ?>
    <table class="table table-striped">
        <thead>
            <tr>
                <th width="10%" align="right">Name</th>
                <th width="10%" align="right">Image</th>
                <th width="20%" align="right">Uploaded Date</th>
                <th width="10%">Status</th>
                <th width="10%" align="right">Action</th>
  </tr>
        </thead>
      
 <?php
        $sql = fetchAll($tableName,'','id',$index,$rpp);
        while ($data = mysql_fetch_assoc($sql)) {
        	$id=$data['id'];
			$status = getStatus($data['status']);
			$status = "<a style=\"text-decoration:none\" href=\"?id=$id&status=$data[status]&page=$currentPage\">$status </a>";
            ?>				<tbody>
                <tr>
                    
                    <td><?= $data['name'] ?></td>
                    <td align="center"><img height="30" width="90" src="images/iphone/<?=$data['image'] ?>"><br/>
    <a target="_blank" href="images/iphone/<?=$data['image'] ?>" >View in Full Size</a>
    </td>
               <td><?= $data['uploaded_date'] ?></td>
               </td><td align="center"><?=$status ?></td>
                    <td><div><a onClick="return confirm('Are you sure to delete?')" href="?mode=del&id=<?=$data['id'] ?>&page=<?=$currentPage ?>" class="delete"><span class="glyphicon glyphicon-trash" style="font-size:30px; color: red;"></span></a>&nbsp;&nbsp;&nbsp;</div>
                        <br>
                       
                    </td>


                <?php } ?>
    </table>
    <ul class="pagination pagination-lg">

  <li class="disabled"><a href="#">&laquo;</a></li>

<?php
  for ($i=1; $i <= $total_pages ; $i++) {
    if($i==$currentPage) {
?>
  <li class="active"><a href="#"><?=$i ?> <span class="sr-only">(current)</span></a></li>

<?php
} else { //end current page if()
?>
  <li><a href="?page=<?=$i ?>"><?=$i ?></a></li>
<?php
} // end else
}//end for() for paging
?>

  <li><a href="#">&raquo;</a></li>
</ul>
                </div>
